package com.personalkeeper;

import java.io.File;

import com.personalkeeper.HistoryProvider.Notes;
import com.personalkeeper.PersonalKeeper.Utils;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NotesCursorAdapter extends CursorAdapter {

    private LayoutInflater mInflater;
    private int mIdIndex;
    private int mTitleIndex;
    private int mDescriptionIndex;
    private int mImagePathIndex;
    private int mLatitudeIndex;
    private int mLongitudeIndex;
    private int mDateIndex;

    public NotesCursorAdapter(Context context, Cursor cur) {
        super(context, cur);
        mInflater = LayoutInflater.from(context);
        mIdIndex = getCursor().getColumnIndexOrThrow(Notes._ID);
        mTitleIndex = getCursor().getColumnIndexOrThrow(Notes.TITLE);
        mDescriptionIndex = getCursor().getColumnIndexOrThrow(Notes.DESCRIPTION);
        mImagePathIndex = getCursor().getColumnIndexOrThrow(Notes.IMAGE_PATH);
        mLatitudeIndex = getCursor().getColumnIndexOrThrow(Notes.LATITUDE);
        mLongitudeIndex = getCursor().getColumnIndexOrThrow(Notes.LONGITUDE);
        mDateIndex = getCursor().getColumnIndexOrThrow(Notes.DATE);
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        bindViewToCursor(holder, cursor);
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = mInflater.inflate(R.layout.note_item, parent, false);
        ViewHolder holder = getHolder(view);
        view.setTag(holder);
        bindViewToCursor(holder, cursor);
        return view;
    }
    private ViewHolder getHolder(View view) {
        ViewHolder holder = new ViewHolder();
        holder.noteTitle = (TextView) view.findViewById(R.id.note_title);
        holder.noteDescription = (TextView) view.findViewById(R.id.note_comment);
        holder.notePhoto = (ImageView) view.findViewById(R.id.note_photo);

        return holder;
    }

    private void bindViewToCursor(ViewHolder holder, Cursor c) {
        holder.id = c.getInt(mIdIndex);
        holder.title = c.getString(mTitleIndex);
        holder.description = c.getString(mDescriptionIndex);
        holder.imagePath = c.getString(mImagePathIndex);
        holder.latitude = c.getDouble(mLatitudeIndex);
        holder.longitude = c.getDouble(mLongitudeIndex);
        holder.date = c.getLong(mDateIndex);

        holder.noteTitle.setText(holder.title); // should always be != null
        if (holder.description != null)
            holder.noteDescription.setText(holder.description);
        if (holder.imagePath != null) {
            File imageFile = new File(holder.imagePath);
            File previewFile = Utils.noteImagePathToPreview(imageFile);
            Drawable image = Drawable.createFromPath(previewFile.toString());
            if (image != null)
                holder.notePhoto.setImageDrawable(image);
            else
                holder.notePhoto.setImageResource(R.drawable.photo_deleted_for_list);
        } else
            holder.notePhoto.setImageResource(R.drawable.no_photo);
    }
    public static class ViewHolder {
        public TextView noteTitle;
        public TextView noteDescription;
        public ImageView notePhoto;

        public long id;
        public String title;
        public String description;
        public String imagePath;
        public double latitude;
        public double longitude;
        public long date;
    }

}
